import tkinter as tk
from tkinter import ttk, messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from input_gui import InputWellGUI

class ResultWellGUI(InputWellGUI):
    def __init__(self, root, calculator):
        super().__init__(root, calculator)
        
        # 初始化结果表格
        self.init_result_tables()
        
        # 初始化绘图区域
        self.init_plot_area()
    
    def init_result_tables(self):
        # 分段结果表格
        segment_columns = ("分段", "底标高 (m)", "顶标高 (m)", "壁厚 (m)", "外径 (m)", 
                          "自重 (kN)", "外表面积 (㎡)", "投影面积 (㎡)")
        self.segment_tree = ttk.Treeview(self.segment_tab, columns=segment_columns, show="headings")
        
        for col in segment_columns:
            self.segment_tree.heading(col, text=col)
            self.segment_tree.column(col, width=100, anchor=tk.CENTER)
        
        self.segment_tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 分节结果表格
        section_columns = ("分节", "底标高 (m)", "顶标高 (m)", "自重 (kN)", 
                          "外表面积 (㎡)", "投影面积 (㎡)")
        self.section_tree = ttk.Treeview(self.section_tab, columns=section_columns, show="headings")
        
        for col in section_columns:
            self.section_tree.heading(col, text=col)
            self.section_tree.column(col, width=100, anchor=tk.CENTER)
        
        self.section_tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
    
    def init_plot_area(self):
        # 增大绘图区域
        self.fig = plt.Figure(figsize=(10, 7), dpi=100)
        self.ax = self.fig.add_subplot(111)
        
        self.canvas = FigureCanvasTkAgg(self.fig, self.plot_tab)
        self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        
        # 添加初始文本说明
        self.ax.text(0.5, 0.5, "点击'计算并绘图'按钮生成标高示意图", 
                    ha='center', va='center', fontsize=12, color='gray')
        self.ax.axis('off')
        self.canvas.draw()
    
    def set_default_values(self):
        # 设置默认值
        self.basic_entries[0].insert(0, "37.782")
        self.basic_entries[1].insert(0, "14.5")
        self.basic_entries[2].insert(0, "22.7")
        self.basic_entries[3].insert(0, "24.7")
        self.basic_entries[4].insert(0, "25")
        
        # 设置分段和分节的默认值
        self.set_segment_default_values()
        self.set_section_default_values()
    
    def set_segment_default_values(self):
        # 分段默认值
        segment_values = [
            ("10", "1.5"),
            ("10", "1.3"),
            ("10", "1.1"),
            ("7.782", "0.9")
        ]
        
        count = min(len(segment_values), len(self.segment_entries))
        for i in range(count):
            length, thickness = segment_values[i]
            self.segment_entries[i][0].delete(0, tk.END)
            self.segment_entries[i][0].insert(0, length)
            self.segment_entries[i][1].delete(0, tk.END)
            self.segment_entries[i][1].insert(0, thickness)
        
        # 对于额外的分段，设置默认值
        for i in range(count, len(self.segment_entries)):
            self.segment_entries[i][0].delete(0, tk.END)
            self.segment_entries[i][0].insert(0, "0")
            self.segment_entries[i][1].delete(0, tk.END)
            self.segment_entries[i][1].insert(0, "0.5")
    
    def set_section_default_values(self):
        # 分节默认值
        section_values = ["8.7", "6", "6", "6", "6", "5.082"]
        
        count = min(len(section_values), len(self.section_entries))
        for i in range(count):
            self.section_entries[i].delete(0, tk.END)
            self.section_entries[i].insert(0, section_values[i])
        
        # 对于额外的分节，设置默认值
        for i in range(count, len(self.section_entries)):
            self.section_entries[i].delete(0, tk.END)
            self.section_entries[i].insert(0, "0")